// 本地存储封装

// 存储键名常量
const STORAGE_KEYS = {
  TOKEN: 'todo_token',
  USER_INFO: 'todo_user_info'
}

/**
 * 设置token
 * @param token token值
 */
export const setToken = (token: string): void => {
  localStorage.setItem(STORAGE_KEYS.TOKEN, token)
}

/**
 * 获取token
 * @returns token值
 */
export const getToken = (): string | null => {
  return localStorage.getItem(STORAGE_KEYS.TOKEN)
}

/**
 * 删除token
 */
export const removeToken = (): void => {
  localStorage.removeItem(STORAGE_KEYS.TOKEN)
}

/**
 * 设置用户信息
 * @param userInfo 用户信息
 */
export const setUserInfo = (userInfo: any): void => {
  localStorage.setItem(STORAGE_KEYS.USER_INFO, JSON.stringify(userInfo))
}

/**
 * 获取用户信息
 * @returns 用户信息
 */
export const getUserInfo = (): any | null => {
  const userInfo = localStorage.getItem(STORAGE_KEYS.USER_INFO)
  return userInfo ? JSON.parse(userInfo) : null
}

/**
 * 删除用户信息
 */
export const removeUserInfo = (): void => {
  localStorage.removeItem(STORAGE_KEYS.USER_INFO)
}

/**
 * 清空所有存储
 */
export const clearStorage = (): void => {
  localStorage.removeItem(STORAGE_KEYS.TOKEN)
  localStorage.removeItem(STORAGE_KEYS.USER_INFO)
}
